﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TuanHuy.DAL.EntityClasses;
using TuanHuy.BLL;

public partial class Admin_EditProject : System.Web.UI.Page
{
    private const string pathUpload = "../Uploads/Projects";
    private const string pathSave = "Uploads/Projects";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadCatolery();
            loadData();
        }
    }

    private void loadData()
    {
        ProjectsLanguageEntity pro = ProjectsLanguageManager.CreateInstant().SelectOne(decimal.Parse(Request["id"]));
        txtProduct.Text = pro.Name;
        txtDescription.Text = pro.Descriptions;
        txtContent.Value = pro.Content;
        imgUpload.ImageUrl = "~/" + pro.Image;
        ddlCat.SelectedValue = pro.CategoryId.ToString();
    }

    private void loadCatolery()
    {
        ddlCat.DataSource = ProjectCatogeryManager.CreateInstant().SelectAllLST().ToList();
        ddlCat.DataTextField = "CatogeryName";
        ddlCat.DataValueField = "CatogeryId";
        ddlCat.DataBind();

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        decimal id = 0;
        string location = "";
        string path = "";
        try
        {
            ProjectsLanguageEntity pro = ProjectsLanguageManager.CreateInstant().SelectOne(decimal.Parse(Request["id"]));
            string nextID = Guid.NewGuid().ToString();
            if ((fuImage.PostedFile != null) && (fuImage.PostedFile.ContentLength > 0))
            {
                string fn = System.IO.Path.GetFileName(fuImage.PostedFile.FileName);

                location = Server.MapPath(pathUpload) + "/" + nextID + fn;

                path = pathSave + "/" + nextID + fn;
                try
                {
                    //fImageUpload.PostedFile.SaveAs(location);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
                    int width = 0;
                    if (image.Size.Width > 1000)
                        width = 1000;
                    else
                        width = image.Size.Width;
                    Common.ResizeImage(image, image.Size.Width, location);
                }
                catch (Exception ex)
                {
                    //Response.Write("Error: " + ex.Message);
                }
                pro.Image = path;
            }



            pro.CategoryId = int.Parse(ddlCat.SelectedValue);
            pro.Name = txtProduct.Text;
            pro.Descriptions = txtDescription.Text;
            pro.Content = txtContent.Value;
            ProjectsLanguageManager.CreateInstant().Update(pro);
        }
        catch (Exception ex) { }
        Response.Redirect("ListProjects.aspx");
    }
}